<?php
/**
 * 登录类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年10月24日 上午11:59:13 
 * 类说明
 */
class LoginAction extends BaseAction{
	public function index(){
		echo $this->err("非法操作！");
	}
	
	/**
	 * 登录接口
	 */
	public function login(){
		$User = M('User');$Fields = M('Fields');
		$param = $this->param;
		if(empty($param['password']) || empty($param['username'])){
			echo $this->err("参数错误！");
			return ;
		}
		$where['username|tel'] = $param['username'];
		$where['flag'] = 1;
		$info = $User->where($where)->find();
		if(count($info) == 0){
			echo $this->err("未找到该用户！");
			return ;
		}
		if($info['password'] != $param['password']){
			echo $this->err("用户名或密码不正确！");
			return ;
		}
		//session管理
		$LoginSession = M('LoginSession');
		$sessionData['user_id'] = $info["id"];
		$sessionId = strtolower(md5(md5(microtime())));
		$sessionData['session_id'] = $sessionId;
		$sessionData['overdue_time'] = time()+(60*24*3600);//60天后过期
		$sessionData['create_time'] = time();
		$is_ex = $LoginSession->where('user_id = '.$info["id"])->count();
		if($is_ex > 0){
			$LoginSession->where('user_id = '.$info["id"])->data($sessionData)->save();
		}else{
			$LoginSession->add($sessionData);
		}
		//准备返回数据
		$backData['userId'] =  $info['id'];
		$backData['sessionId'] = $sessionId;
		$backData['tel'] =  $info['tel'];
		$backData['photo'] =  $info['photo'];
		if($info['phone_manage']==1){
			$backData['is_shop'] =  1;
		}else{
			$backData['is_shop'] =  $info['is_shop'];
		}

		//返回电话
		$backData['customTel'] = $Fields->where('flag = 1 and field = "app_custom_tel"')->getField('value');
		$User->where($where)->setField('jpush_id',$param['jpushId']);//设置极光推送ID
		echo $this->suc($backData);
	}
	
	/**
	 * 退出
	 * Enter description here ...
	 */
	public function logout(){
		$LoginSession = M('LoginSession');$User = M('User');
		$param = $this->param;
		if(!empty($param['userId']) && !empty($param['sessionId'])){
			$where['session_id'] = $param['sessionId'];
			$where['user_id'] = $param['userId'];
			$LoginSession->where($where)->delete();
			//清空极光推送ID
			$User->where('id = '.$param['userId'])->setField('jpush_id','');
		}
	}

	/**
	 * 注册并登录
	 */
	public function register(){
		$User = M("User");$Fields = M('Fields');
		$param = $this->param;
		if(empty($param['phoneCode'])){
			echo $this->err('验证码为空！');
			return ;
		}
		if($param['tel'] != session('send_code_tel')){
			echo $this->err("请重新获取验证码！");
			return ;
		}
		if($param['phoneCode'] != session('tel_code')){
			echo $this->err("验证码错误！");
			return ;
		}
		if(!checkPhone($param['tel'])){
			echo $this->err("手机号码格式错误！");
			return ;
		}

		//验证手机号唯一
		$is_ex = $User->where('tel = "'.$param['tel'].'" and flag = 1 ')->count();
		if($is_ex > 0){
			echo $this->err("该手机号码已注册，可直接登录！");
			return ;
		}
		//判断推荐人是否存在
		if(!empty($param['parentId'])){
			if(!is_numeric($param['parentId'])){
				echo $this->err('推荐人ID为非负整数！');
				return ;
			}
			$count = $User->where('flag = 1 and id = '.$param['parentId'])->count();
			if($count == 0){
				echo $this->err('推荐人ID不存在！');
				return ;
			}
		}
		$data['username'] = $param['tel'];
		$data['password'] = $param['password'];//客户端加密
		$data['tel'] = $param['tel'];
		$data['parent_id'] = $param['parentId'];
		$data['create_time'] = time();//获取当前时间戳
		$data['jpush_id'] = $param['jpushId'];//极光推送ID
		$lastInsId  = $User->data($data)->add();
		if($lastInsId){
			//清空手机验证码的缓存时间和验证码
			session("send_code_time",null);
			session('tel_code',null);
			//session管理
			$LoginSession = M('LoginSession');
			$sessionData['user_id'] = $lastInsId;
			$sessionId = strtolower(md5(md5(microtime())));
			$sessionData['session_id'] = $sessionId;
			$sessionData['overdue_time'] = time()+(60*24*3600);//60天后过期
			$sessionData['create_time'] = time();
			$is_ex = $LoginSession->where('user_id = '.$lastInsId)->count();
			if($is_ex > 0){
				$LoginSession->where('user_id = '.$lastInsId)->data($sessionData)->save();
			}else{
				$LoginSession->add($sessionData);
			}
			//准备返回数据
			$backData['userId'] =  $lastInsId;
			$backData['sessionId'] = $sessionId;
			$backData['tel'] =  $param['tel'];
			$backData['photo'] =  "";//头像问题后期考虑
			$backData['is_shop'] =  0; //新注册用户没有店铺
			//返回电话
			$backData['customTel'] = $Fields->where('flag = 1 and field = "app_custom_tel"')->getField('value');
			echo $this->suc($backData);
		}else {
			echo $this->err("系统繁忙！");
			return ;
		}
	}
	
	/**
	 * 发送短信
	 * 不考虑锁(islock)的问题
	 */
	public function sendPhoneCode(){
		$param = $this->param;
		$tel = $param['tel'];
		if(!checkPhone($tel)){
			echo $this->err("错误的手机号！");
			return ;
		}
		$tel_code = rand(100001,999999);
		if((session("send_code_time") + 60) > time() && isset($_SESSION['send_code_time'])) {//将获取的缓存时间转换成时间戳加上60秒后与当前时间比较，小于当前时间为频繁获取
			echo $this->err("请勿频繁获取验证码！");
			return;
		}else{
			import('COM.Core.SendMsg');
			$SendMsg = new SendMsg();
			$content = '您的验证码是['.$tel_code.']';
			$_SESSION['tel_code'] = $tel_code;
			$result = $SendMsg->sendMsg($tel, $content);
			session('send_code_time',time()); // 发送成功后记录当前发送的时间
			session('send_code_tel',$tel);
			echo $this->suc();
		}
	}
	
	/**
	 * 找回登录密码
	 * Enter description here ...
	 */
	public function findPwd(){
		$User = M('User');
		$param = $this->param;
		if(!checkPhone($param['tel'])){
			echo $this->err("错误的手机格式！");
			return ;
		}
		if(empty($param['phoneCode'])){
			echo $this->err('验证码为空！');
			return ;
		}
		if($param['tel'] != session('send_code_tel')){
			echo $this->err("请重新获取验证码！");
			return ;
		}
		if($param['phoneCode'] != session('tel_code')){
			echo $this->err("验证码错误！");
			return ;
		}
		$where['flag'] = 1;
		$where['tel'] = $param['tel'];
		$info = $User->where($where)->find();
		if(empty($info)){
			echo $this->err("未找到该用户！");
			return ;
		}
		$data['password'] = $param['password'];
		$data['update_time'] = time();
		$result = $User->where($where)->save($data);
		if($result){
			session("send_code_time",null);
			session('tel_code',null);
			echo $this->suc();
		}else {
			echo $this->err("修改失败！");
		}
	}
}